package com.controller;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dao.AppClassDao;
import com.dao.ClassroomDao;
import com.dao.ClassroomTimeDao;
import com.dao.StudentDao;
import com.dao.TeacherDao;
import com.mybatis.po.AppClass;
import com.mybatis.po.ClassroomTime;
import com.mybatis.po.Student;

@Controller
public class AppClassController {
	
	@Autowired
	public StudentDao studentDao ;
	@Autowired
	public TeacherDao teacherDao ;
	@Autowired
	public AppClassDao appClassDao ;
	@Autowired
	public ClassroomDao classroomDao;
	@Autowired
	public ClassroomTimeDao classroomTimeDao;
	// 显示相关信息接口
	@RequestMapping("/findThisAppointmentClass")
	@ResponseBody
	public Map<String, String> seeLess(HttpServletRequest request) {
		// 获取传进来的值
		String openid = request.getParameter("openid");
		String state = request.getParameter("state");
		String ifseeAll = request.getParameter("ifseeAll");
		
 		Map<String,String> map = new HashMap<String, String>();
 		// 查找是否有这个用户
		List<Student> listStu = studentDao.selectStudentbyOpenid(openid);
		// 是学生
		if(listStu.size()!=0) {
			// 查找学生的预约记录
			List<AppClass> listAppClass = appClassDao.selectAppClassbyAccountAndState(listStu.get(0).getSno(), state);
			// 该学生有预约记录
			if(listAppClass.size()!=0 ) {
				// 如果ifseeAll为1，则查看详细信息
				if(ifseeAll.equals("1")) {
					map.put("sno", listAppClass.get(0).getSno());// 学生学号
					map.put("sname", listStu.get(0).getSnoName());// 学生姓名
					map.put("tno", listAppClass.get(0).getTno());// 指导老师工号
					map.put("tname", teacherDao.selectTeacherbyAccount(listAppClass.get(0).getTno()).get(0).getTname());// 指导老师名字
					map.put("Ccode", listAppClass.get(0).getCcode());// 教室名字
					map.put("time",listAppClass.get(0).getAppDate()+" "+listAppClass.get(0).getTimeSlot());// 预约时间
					map.put("purpose",listAppClass.get(0).getPurpose());// 预约原因
					map.put("udepartment", listAppClass.get(0).getUdepartment());// 使用组织
					map.put("useEquipment", listAppClass.get(0).getUseEquipment());// 是否使用设备
					map.put("sphoneNum", listAppClass.get(0).getSphoneNum());// 学生电话
					map.put("state", listAppClass.get(0).getState());// 状态
				}
				// 否则只看初略信息
				else {
					map.put("name", listStu.get(0).getSnoName());// 预约人姓名
					map.put("deptName",listAppClass.get(0).getUdepartment());// 预约人单位
					map.put("purpose",listAppClass.get(0).getPurpose());// 预约是由
					map.put("state",state);// 状态
					map.put("time",listAppClass.get(0).getAppDate()+" "+listAppClass.get(0).getTimeSlot());// 预约时间
				}
			}else {
				map.put("error", "你没有预约记录，请预约！");//预约人姓名
			}
		}
		return map;
	}
	// 查询教师预约通过的
		@RequestMapping("/selectTeacherApp")
		@ResponseBody
		public Map<String, List> selectTeacherApp(HttpServletRequest request) {
			// 获取传进来的值
			
			List<String []> nameList = new ArrayList<String []>();
//			nameList.add(studentDao.selectStudentbyOpenid(openid);
//			nameList.put("tname", );// 指导老师名字
//			nameList.add(teacherDao.selectTeacherbyAccount(listAppClass.get(0).getTno()).get(0).getTname())
			String Tno = request.getParameter("Tno");	
		 	Map<String,List> map = new HashMap<String, List>();
		 	
		 	List<AppClass>listAppClass = appClassDao.selectTeacherApp(Tno);
		 	System.out.print("长度是#######"+listAppClass.size());
		 	for (AppClass appClass :listAppClass) {
		 		String [] nameArr = new String [2];
		 		System.out.print("循环"+appClass.toString());
		 		nameArr[0] = studentDao.selectStudentbyAccount(appClass.getSno()).get(0).getSnoName();
		 		nameArr[1] = teacherDao.selectTeacherbyAccount(appClass.getTno()).get(0).getTname();
				nameList.add(nameArr);
			}
			if(listAppClass.size() != 0 ) {
				map.put("appList", listAppClass);
				map.put("nameList", nameList);
			}else {
				ArrayList <String>errorList = new ArrayList <String>();
				errorList.add("not Record");
				map.put("appList", errorList);
			}	
			return map;
		}	
	
	//查看学生所有完成预约记录
	@RequestMapping("/findAllStuCompleteRecord")
	@ResponseBody
	public Map<String, List> findAllStuCompleteRecord(HttpServletRequest request) {
		// 获取传进来的值
		String openid = request.getParameter("openid");
		String state = request.getParameter("state");
 		Map<String,List> map = new HashMap<String, List>();
 		// 查找是否有这个用户
		List<Student> listStu = studentDao.selectStudentbyOpenid(openid);
		// 是学生
		if(listStu.size()!=0) {
			// 通过学生账号和记录状态查找学生的预约记录
			List<AppClass> listAppClass = appClassDao.selectAppClassbyAccountAndState(listStu.get(0).getSno(), state);
			// 该学生有预约记录
			if(listAppClass.size() !=0 ) {
				map.put("appList", listAppClass);
				System.out.print("21231231231"+listAppClass.get(0));
			}else {
				ArrayList <String>errorList = new ArrayList <String>();
				errorList.add("not Record");
				map.put("appList", errorList);
			}
		}
		return map;
	}	
	@RequestMapping("/passAppClass")
	@ResponseBody
	public Map<String, String> passAppClass(AppClass appClass) {
		// 获取传进来的值
		
 		Map<String,String> map = new HashMap<String, String>();
 		// 查找是否有这个用户
 		appClass.setState("1");
		if(appClass.getCcode() != null) {
			if(appClassDao.pass(appClass) == 1) {
				map.put("state", "1");
				map.put("mesage", "Success");
			}else {
				map.put("state", "0");
				map.put("mesage", "Fail");
			}
		}

		return map;
	}	
	//查询所有未审批过的预约的记录
	@RequestMapping("/findAllStuNotCompleteRecord")
	@ResponseBody
	public Map<String, List> seeAllLess(HttpServletRequest request) {
 		Map<String,List> map = new HashMap<String, List>();
 		//查询所有预约状态为申请中的预约记录
 		List<AppClass> listAppClass = appClassDao.findAllStuNotCompleteRecord("1");
 		if(listAppClass.size() != 0) {//有记录则将记录保存在listAppClass，存在map中返回
 			map.put("appList", listAppClass);	
 		}else {//一条记录都没有则返回无记录
 			ArrayList <String> errorList = new ArrayList <String>();
			errorList.add("not any Record");
			map.put("appList", errorList);
 		}
		return map;
	}	
	//提交预约
		@RequestMapping("/CommitAppClass")
		@ResponseBody
		public Map<String, String> CommitLess(AppClass appClass) {
			Map<String,String> map = new HashMap<String, String>();//用于返回提交状态的map
			String slotTime;
			ClassroomTime t = new ClassroomTime();//设置用于查询教室时间的实体类
			t.setCcode(appClass.getCcode());//
			t.setCDate(appClass.getAppDate());
			System.out.print(appClass.toString());
			slotTime = classroomTimeDao.selectClassroomByDateAndCode(t);//获取原本的教室当日时间表
			if(slotTime !=null) {//如果时间不是空就在后面加上预约的时间段
				switch(appClass.getTimeSlot()) {
				case"上午"://每个时间段插入不同的字段
					t.setCTimeSlot(slotTime+"|12|34");
					classroomTimeDao.updateClassroomByDateAndCode(t);//更新教室时间表
					if(appClassDao.CreateAppClass(appClass) == 1) {//插入记录返回1，则说明插入成功
						map.put("state", "1");
						map.put("message","entrySuccess");
					}else {//插入失败，返回fail
						map.put("state", "0");
						map.put("message","entryFail");
					}
					break;
				case"下午":
					t.setCTimeSlot(slotTime+"|56|78");//每个时间段插入不同的字段
					classroomTimeDao.updateClassroomByDateAndCode(t);
					if(appClassDao.CreateAppClass(appClass) == 1) {//插入记录返回1，则说明插入成功
						map.put("state", "1");
						map.put("message","entrySuccess");
					}else {//插入失败，返回fail
						map.put("state", "0");
						map.put("message","entryFail");
					}
					break;
				case"傍晚":
					t.setCTimeSlot(slotTime+"|910|1112");
					classroomTimeDao.updateClassroomByDateAndCode(t);
					appClassDao.CreateAppClass(appClass);
					break;
				case"白天":
					t.setCTimeSlot(slotTime+"|12|34|56|78");
					classroomTimeDao.updateClassroomByDateAndCode(t);
					if(appClassDao.CreateAppClass(appClass) == 1) {//插入记录返回1，则说明插入成功
						map.put("state", "1");
						map.put("message","entrySuccess");
					}else {//插入失败，返回fail
						map.put("state", "0");
						map.put("message","entryFail");
					}
					break;
				case"整天":
					t.setCTimeSlot(slotTime+"|12|34|56|78|910|1112");
					classroomTimeDao.updateClassroomByDateAndCode(t);
					if(appClassDao.CreateAppClass(appClass) == 1) {//插入记录返回1，则说明插入成功
						map.put("state", "1");
						map.put("message","entrySuccess");
					}else {//插入失败，返回fail
						map.put("state", "0");
						map.put("message","entryFail");
					}
					break;
					default:
					t.setCTimeSlot(appClass.getTimeSlot()+slotTime);
					classroomTimeDao.updateClassroomByDateAndCode(t);
					if(appClassDao.CreateAppClass(appClass) == 1) {//插入记录返回1，则说明插入成功
						map.put("state", "1");
						map.put("message","entrySuccess");
					}else {//插入失败，返回fail
						map.put("state", "0");
						map.put("message","entryFail");
					}
					break;
				}
				System.out.print("WQQWEQEQW"+appClass.getTimeSlot());
			}
			return map;
		}
}
